package ControlLayer;
import java.util.ArrayList;
import TUILayer.DvdUI;
import ModelLayer.*;

import java.util.Date;
import java.util.GregorianCalendar;
//model layer package has to be imported
/**
 * Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class DvdCtr
{
    //declare instance variables to reference containers
    //passed as arguments from the main class
    private DvdUI dvdUI;
    private Dvd dvdModel;
    private DvdCopy dvdCopyModel;
    private DvdCollection dvdCollectionModel;

    public DvdCtr()//DVD container is to be passed as parameter from the main controller
    {
        this.dvdUI = new DvdUI();
        this.dvdModel = new Dvd();
        this.dvdCopyModel = new DvdCopy();
        this.dvdCollectionModel = DvdCollection.getInstance();
        dvdMenu();
    }
    
    public void dvdMenu() {
        boolean exit = false;
        
        while(!exit) {
            this.dvdUI.writeDVDMenu();
            int choise = dvdUI.readInt();
        
            if(choise == 1) {
                findDVD();
            } else if(choise == 2) {
                createDVD();
            } else if(choise == 3) {
                createCopy();
            } else if(choise == 4) {
                deleteDVD();
            } else if(choise == 5) {
                listAllDvds();
            } else {
                exit = true;
            }
            
        }
    }
    
    public Dvd getDvd(int id) {
        Dvd dvd = dvdCollectionModel.findDvd(id);
        return dvd;
    }       
    
    public void findDVD()
    {
        int id = dvdSelect();
        Dvd dvd = dvdCollectionModel.findDvd(id);
        if(dvd != null) 
        {
            dvdUI.writeDvdCollection(dvd);
            dvdUI.writeEnterToContinue();
            dvdUI.readLine();
        }
    }
    
    //Dummy implementation: more parameters are probaly to added
    public void createDVD()
    {       
        dvdUI.clearConsole();
        dvdUI.writeCreateNewDvd();
        
        // ID, int
        dvdUI.writeInputDvdID();
        int id = dvdUI.readInt();
        
        // Ttile, String
        dvdUI.writeInputTitle();
        String title = dvdUI.readLine();
        
        // Artist, String
        dvdUI.writeInputArtist();
        String artist = dvdUI.readLine();
        
        // Release Date, String
        dvdUI.writeInputReleaseDate();
        String date = dvdUI.readLine();
               
        // Creates a new person object
        Dvd dvd = new Dvd(id, title, artist, date);

        // Saves person in addressbook.
        dvdCollectionModel.addDvd(dvd);
        
        dvdUI.writeBlankLine();
        dvdUI.writeSaveOK();
        dvdUI.writeEnterToContinue();
        dvdUI.readLine();
    }
        
    public void createCopy()
    {
        int id = dvdSelect();

        Dvd dvd = dvdCollectionModel.findDvd(id);
        if(dvd != null)
        {                    
            dvdUI.writeInputSerial();
            String serial = dvdUI.readLine();
            
            dvdUI.writeInputReqDate();
            String reqDate = dvdUI.readLine();
            
            dvdUI.writeInputBuyPrice();
            double buyPrice = dvdUI.readDouble();
        
        
            DvdCopy newCopy = new DvdCopy(serial, reqDate, buyPrice);
            dvd.addCopy(newCopy);
    
            dvdUI.writeBlankLine();
            dvdUI.writeSaveOK();
            dvdUI.writeEnterToContinue();
            dvdUI.readLine();
        }
    }
    
    public void deleteDVD()
    {
        int id = dvdSelect();
        
        Dvd dvd = dvdCollectionModel.findDvd(id);
        if(dvd != null) 
        {
            dvdCollectionModel.deleteDvd(id);
        }
    }
    
    private int dvdSelect()
    {
        int id = 0;
        dvdUI.writeDvdCollection();
        ArrayList<Dvd> d = dvdCollectionModel.getAllDvds();
        dvdUI.writeOutDvd(d);
        
        if(d.size() > 0) {
            dvdUI.writeSelectDvdId();
            id = dvdUI.readInt();
        } else {
            dvdUI.writeEnterToContinue();
            dvdUI.readLine();
        }
        
        return id;
    }    
    
    public void listAllDvds()
    {
        dvdUI.writeDvdCollection();
        dvdUI.writeOutDvd(dvdCollectionModel.getAllDvds());
        
        dvdUI.writeBlankLine();
        dvdUI.writeEnterToContinue();
        dvdUI.readLine();
    }
    
    public boolean returnedTooLate(String date, int period) {
        // Making sure that the data is somewhat right
        if(date.length() == 10 && period > 0) {
        
            // Substrings the year, month and day from the string
            int year = Integer.parseInt(date.substring(6, 10));
            int month = Integer.parseInt(date.substring(3, 5)) - 1;
            int day = Integer.parseInt(date.substring(0, 2));
            
            // Makeing a new Gregorian Calendar from the dates and make it a date-type
            Date d1 = new GregorianCalendar(year, month, day).getTime();
            // Getting the timestamp for right now
            Date today = new Date();
            
            // Taking the UNIX-TIME in miliseconds and substract it
            // Also making the miliseconds to days
            long diff = (today.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
            
            // Checking if the difference between the allowed period and the diffrence is bigger than 0
            if(period-diff >= 0) {
                // Returning false, it wasn't too late
                return false;
            } else {
                // Returning true, it is too late
                return true;
            }
        } else {
            // Just tells that it was too late (fuck the customers)!!
            return true;
        }
    }
}